#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution {
    public:
        bool searchMatrix(vector<vector<int> > &matrix, int target) {
            if (matrix.size() == 0) return false;
            int lo, hi;
            int row = matrix.size(), col = matrix[0].size();
            if (col == 0) return false;
            lo = 0;
            hi = row*col -1;
            while(lo <= hi) {
                int mid = lo + (hi-lo)/2;
                if (matrix[mid/col][mid%col] == target) return true;
                else if (matrix[mid/col][mid%col] < target) lo = mid+1;
                else hi = mid -1;
            }
            return false;                        
        }
};

//test on 1337 online judge and pass all the test
int main(int argc, char **argv)
{
    Solution mySolution;
    vector<vector<int> > matrix, matrix1;
    vector<int> row1, row2, row3;
    row1.push_back(1);
    row1.push_back(3);
    row1.push_back(5);
    row1.push_back(7);
    row2.push_back(10);
    row2.push_back(11);
    row2.push_back(16);
    row2.push_back(20);
    row3.push_back(23);
    row3.push_back(30);
    row3.push_back(34);
    row3.push_back(50);
    matrix.push_back(row1);
    matrix.push_back(row2);
    matrix.push_back(row3);

    cout << "-----------------Test 1--------------------" << endl;
    cout << mySolution.searchMatrix(matrix, 1) << endl;
    cout << mySolution.searchMatrix(matrix, 3) << endl;
    cout << mySolution.searchMatrix(matrix, 40) << endl;
    cout << mySolution.searchMatrix(matrix, 50) << endl;
    cout << mySolution.searchMatrix(matrix, 22) << endl;
    cout << mySolution.searchMatrix(matrix1, 22) << endl;


    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
